package com.itfitness.nervecat.activitys;

import android.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.itfitness.nervecat.R;
import com.itfitness.nervecat.gamewidget.GameCallback;
import com.itfitness.nervecat.gamewidget.GameView;

public class MainActivity extends AppCompatActivity {
    private GameView activityMainGameview;
    private AlertDialog mAlertDialogLose;
    private AlertDialog mAlertDialogWin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        activityMainGameview = (GameView) findViewById(R.id.activity_main_gameview);
        activityMainGameview.setGameCallback(new GameCallback() {
            @Override
            public void onLose() {
                //输了
                showLoseDialog();
            }

            @Override
            public void onWin(int stepnum) {
                //赢了
                showWinDialog(stepnum);
            }
        });
    }
    /**
     * 展示赢了的弹窗
     */
    private void showWinDialog(int stepnum) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        View inflate = View.inflate(this, R.layout.dialog_win, null);
        ImageView dialogFaildCancle =  (ImageView) inflate.findViewById(R.id.dialog_win_cancle);
        ImageView dialogFaildReplay = (ImageView) inflate.findViewById(R.id.dialog_win_replay);
        TextView textViewWin = (TextView) inflate.findViewById(R.id.dialog_win_redtext);
        textViewWin.setText("您用了"+stepnum+"步围住了萌萌");
        if(mAlertDialogWin==null){
            mAlertDialogWin = builder.setView(inflate).create();
            dialogFaildCancle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mAlertDialogWin.dismiss();
                }
            });
            dialogFaildReplay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mAlertDialogWin.dismiss();
                    activityMainGameview.initGame();
                }
            });
            mAlertDialogWin.show();
//设置背景透明
            mAlertDialogWin.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        }else {
            mAlertDialogWin.show();
            mAlertDialogWin.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        }
    }

    /**
     * 展示输了的弹窗
     */
    private void showLoseDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        View inflate = View.inflate(this, R.layout.dialog_faild, null);
        ImageView dialogFaildCancle =  (ImageView) inflate.findViewById(R.id.dialog_faild_cancle);
        ImageView dialogFaildReplay = (ImageView) inflate.findViewById(R.id.dialog_faild_replay);
        if(mAlertDialogLose==null){
            mAlertDialogLose = builder.setView(inflate).create();
            dialogFaildCancle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mAlertDialogLose.dismiss();
                }
            });
            dialogFaildReplay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mAlertDialogLose.dismiss();
                    activityMainGameview.initGame();
                }
            });
            mAlertDialogLose.show();
//设置背景透明
            mAlertDialogLose.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        }else {
            mAlertDialogLose.show();
            mAlertDialogLose.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        }
    }
}
